#include "tempo_dialog.h"

#include <QGroupBox>
#include <QHBoxLayout>
#include <QPushButton>
#include <QRadioButton>
#include <QVBoxLayout>

#include "core/music_settings.h"

TempoDialog::TempoDialog(MusicSettings *settings, QWidget *parent):
  QDialog(parent),
  settings(settings) {
  setWindowTitle("Tempo");
  QGroupBox *tempoBox = new QGroupBox("Tempo", this);
  QVBoxLayout *tempoBoxLayout = new QVBoxLayout;
  tempoBox->setLayout(tempoBoxLayout);

  largo = new QRadioButton("Largo - 60", this);
  tempoBoxLayout->addWidget(largo);

  larghetto = new QRadioButton("Larghetto - 66", this);
  tempoBoxLayout->addWidget(larghetto);

  adagio = new QRadioButton("Adagio - 76", this);
  tempoBoxLayout->addWidget(adagio);

  andante = new QRadioButton("Andante - 108", this);
  tempoBoxLayout->addWidget(andante);

  moderato = new QRadioButton("Moderato - 120", this);
  tempoBoxLayout->addWidget(moderato);

  allegro = new QRadioButton("Allegro - 168", this);
  tempoBoxLayout->addWidget(allegro);

  presto = new QRadioButton("Presto - 200", this);
  tempoBoxLayout->addWidget(presto);

  prestissimo = new QRadioButton("Prestissimo - 208", this);
  tempoBoxLayout->addWidget(prestissimo);

  // boutons

  QPushButton *okButton = new QPushButton("&Valider", this);
  connect(okButton, SIGNAL(clicked()), this, SLOT(validate()));

  QPushButton *cancelButton = new QPushButton("&Annuler", this);
  connect(cancelButton, SIGNAL(clicked()), this, SLOT(close()));

  QHBoxLayout *buttonLayout = new QHBoxLayout;
  buttonLayout->addStretch();
  buttonLayout->addWidget(okButton);
  buttonLayout->addWidget(cancelButton);

  //

  QVBoxLayout *mainLayout = new QVBoxLayout;
  mainLayout->addWidget(tempoBox);
  mainLayout->addLayout(buttonLayout);
  setLayout(mainLayout);

  initialize();
}

/*!
 * Initialise le bon bouton en fonction des paramètres
 */
void TempoDialog::initialize() {
  switch(settings->tempo) {
    case MusicSettings::LARGO:
      largo->setChecked(true);
      return;
    case MusicSettings::LARGHETTO:
      larghetto->setChecked(true);
      return;
    case MusicSettings::ADAGIO:
      adagio->setChecked(true);
      return;
    case MusicSettings::ANDANTE:
      andante->setChecked(true);
      return;
    case MusicSettings::MODERATO:
      moderato->setChecked(true);
      return;
    case MusicSettings::ALLEGRO:
      allegro->setChecked(true);
      return;
    case MusicSettings::PRESTO:
      presto->setChecked(true);
      return;
    case MusicSettings::PRESTISSIMO:
      prestissimo->setChecked(true);
      return;
    default:
      return;
  }
}

/*!
 * Change les paramètres du tempo en fonction du bouton sélectionné
 * puis ferme la fenêtre
 */
void TempoDialog::validate() {
  if(largo->isChecked())
    settings->tempo = MusicSettings::LARGO;
  else if(larghetto->isChecked())
    settings->tempo = MusicSettings::LARGHETTO;
  else if(adagio->isChecked())
    settings->tempo = MusicSettings::ADAGIO;
  else if(andante->isChecked())
    settings->tempo = MusicSettings::ANDANTE;
  else if(moderato->isChecked())
    settings->tempo = MusicSettings::MODERATO;
  else if(allegro->isChecked())
    settings->tempo = MusicSettings::ALLEGRO;
  else if(presto->isChecked())
    settings->tempo = MusicSettings::PRESTO;
  else if(prestissimo->isChecked())
    settings->tempo = MusicSettings::PRESTISSIMO;

  close();
}
